package stark.app.function.game;

import com.stark.game.dice.DiceContainer;

import stark.app.R;
import stark.app.databinding.FragmentDiceDemoBinding;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * Author：daizhongbin on 2023/6/15
 * Email：1027154338@qq.com
 * Desc:
 */
public class DiceDemoFragment extends BaseNoModelFragment<FragmentDiceDemoBinding> {

    @Override
    protected int onCreate() {
        return R.layout.fragment_dice_demo;
    }

    @Override
    protected void initView() {
        //设置最多可放置10个骰子
        mDataBinding.diceContainer.setMaxDiceCount(10);
        //骰子+1
        mDataBinding.btnAddDice.setOnClickListener(v -> {
            mDataBinding.diceContainer.setDiceCount(mDataBinding.diceContainer.getDiceCount() + 1);
        });
        //骰子-1
        mDataBinding.btnReduceDice.setOnClickListener(v -> {
            mDataBinding.diceContainer.setDiceCount(mDataBinding.diceContainer.getDiceCount() - 1);
        });
        //允许锁定
        updateBtnAllowLock();
        mDataBinding.btnAllowLock.setOnClickListener(v -> {
            mDataBinding.diceContainer.setAllowLock(!mDataBinding.diceContainer.isAllowLock());
            updateBtnAllowLock();
        });
        //骰子出来方向
        updateBtnOutPos();
        mDataBinding.btnOutPos.setOnClickListener(v -> {
            DiceContainer.StarkOutPos newOutPos = DiceContainer.StarkOutPos.leftTop;
            switch (mDataBinding.diceContainer.getStarkOutPos()) {
                case leftTop:
                    newOutPos = DiceContainer.StarkOutPos.leftBottom;
                    break;
                case leftBottom:
                    newOutPos = DiceContainer.StarkOutPos.rightTop;
                    break;
                case rightTop:
                    newOutPos = DiceContainer.StarkOutPos.rightBottom;
                    break;
                case rightBottom:
                    newOutPos = DiceContainer.StarkOutPos.leftTop;
                    break;
            }
            mDataBinding.diceContainer.setStarkOutPos(newOutPos);
            updateBtnOutPos();
        });
        //投掷骰子
        mDataBinding.btnPlay.setOnClickListener(v -> {
            mDataBinding.diceContainer.playDice(new DiceContainer.IPlayDiceListener() {
                @Override
                public void onStart(int totalDiceNumber) {
                    mDataBinding.tvTotalNumber.setText("骰子点数和为：" + totalDiceNumber);
                    //如果不想使用库内部默认的骰子声音，可以在这个方法里播放自己想要的声音，同时请记得将内部的声音设为关闭状态
                    //mDataBinding.diceContainer.setPlaySound(false); //将内部默认的投掷骰子声关闭掉
                }
            });
        });
    }

    @Override
    protected void initData() {

    }

    private void updateBtnAllowLock() {
        mDataBinding.btnAllowLock.setText(mDataBinding.diceContainer.isAllowLock() ? "允许锁定" : "不允许锁定");
    }

    private void updateBtnOutPos() {
        StringBuilder builder = new StringBuilder();
        builder.append("骰子出来方向：");
        switch (mDataBinding.diceContainer.getStarkOutPos()) {
            case leftTop:
                builder.append("左上");
                break;
            case rightTop:
                builder.append("右上");
                break;
            case leftBottom:
                builder.append("左下");
                break;
            case rightBottom:
                builder.append("右下");
                break;
        }
        mDataBinding.btnOutPos.setText(builder.toString());
    }
}
